package 题目集.递推;

/**
 * https://leetcode.cn/problems/minimum-operations-to-make-binary-array-elements-equal-to-one-i/description/?envType=daily-question&envId=2024-10-18
 */
public class ch02_使二进制数组全部等于1的最少操作次数_1 {
    public int minOperations(int[] nums) {
        int len = nums.length - 2;
        int cnt = 0;
        for (int i = 0; i < len; i++) {
            if (nums[i] == 0) {
                nums[i] ^= 1;
                nums[i + 1] ^= 1;
                nums[i + 2] ^= 1;
                cnt++;
            }
        }
        while (len < nums.length) {
            if (nums[len++] == 0) return -1;
        }
        return cnt;
    }
}
